package com.action;

import com.pojo.Demo;
import com.pojo.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@Controller
@RequestMapping("/")
public class TestController {

    @GetMapping("j/dialogParent")
    public String dialogParent(Model model) {
        //List<Demo> jspList = getList("jsp");
        //model.addAttribute("list", jspList);

        return "jsp/dialogParent";
    }

    @RequestMapping("j/dialog")
    public String dialog(Model model, @RequestParam Map<String, Object> params) {
        //接收参数进行查询
        String name = (String) params.get("name");
        List<Demo> jspList = getList(name);
        model.addAttribute("list", jspList);
        return "jsp/dialog";
    }

    @GetMapping("j/list")
    public String jList(Model model) {
        List<Demo> jspList = getList("jsp");
        model.addAttribute("list", jspList);
        return "jsp/index";
    }

    @GetMapping("t/list")
    public String tList(Model model) {
        List<Demo> jspList = getList("html");
        model.addAttribute("list", jspList);
        return "thymeleaf/index";
    }

    @GetMapping("f/list")
    public String fList(Model model) {
        List<Demo> jspList = getList("free");
        model.addAttribute("list", jspList);
        return "freemarker/index";
    }

    @GetMapping("j/listPage")
    public String jlistPage(Model model, @RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if(null == pageNo || null == pageSize){
            pageNo = 1;
            pageSize = 10;
        }
        List<Demo> pageList = getPageList("jsp_pageList", pageNo, pageSize);
        Integer count = getCount();
        Page page = new Page();
        page.setCurrentPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(pageList);
        page.setCount(count);
        model.addAttribute("page", page);
        return "jsp/listPage";
    }

    @GetMapping("t/listPage")
    public String tlistPage(Model model, @RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if(null == pageNo || null == pageSize){
            pageNo = 1;
            pageSize = 10;
        }
        List<Demo> pageList = getPageList("html_pageList", pageNo, pageSize);
        Integer count = getCount();
        Page page = new Page();
        page.setCurrentPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(pageList);
        page.setCount(count);
        model.addAttribute("page", page);
        return "thymeleaf/listPage";
    }

    @GetMapping("f/listPage")
    public String flistPage(Model model, @RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if(null == pageNo || null == pageSize){
            pageNo = 1;
            pageSize = 10;
        }
        List<Demo> pageList = getPageList("html_pageList", pageNo, pageSize);
        Integer count = getCount();
        Page page = new Page();
        page.setCurrentPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(pageList);
        page.setCount(count);
        model.addAttribute("page", page);
        return "freemarker/listPage";
    }

    private Integer getCount(){
        return 50;
    }

    private List<Demo> getPageList(String name, Integer pageNo, Integer pageSize) {
        return IntStream.range(1, 11).mapToObj(i -> new Demo((pageNo-1)*pageSize + i + "", name + "_name_" + i)).toList();
    }

    private List<Demo> getList(String name) {
        return IntStream.range(1, 10).mapToObj(i -> new Demo(i+"", name + "_name_" + i)).toList();
    }

}
